package app.msy.cn.rxandroid;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import app.msy.cn.rxandroid.bean.LoginResp;
import app.msy.cn.rxandroid.ui.activity.LoginActivity;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class LaunchActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch);

        Observable
                .timer(2, TimeUnit.SECONDS, AndroidSchedulers.mainThread())
                .map(new Func1<Long, Object>() {
                    @Override
                    public Object call(Long aLong) {
                        Intent intent = new Intent(LaunchActivity.this, LoginActivity.class);
                        startActivity(intent);
                        finish();
                        return null;
                    }
                }).subscribe();

        Observable.just(1, 2, 3, 4)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        Log.d("TAG", "call: " + integer);
                    }
                });

        //map操作符
        Observable.just("1").map(new Func1<String, Integer>() {
            @Override
            public Integer call(String s) {
                return Integer.parseInt(s);
            }
        }).subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer integer) {
                integer++;
                Log.d("TAG", "call: " + integer);
            }
        });

        //flatmap操作符
        //Returns an Observable that emits a single item and then completes.
        //Converts an Iterable sequence into an Observable that emits the items in the sequence.
        List<LoginResp> list = new ArrayList<>();
        Observable.from(list).flatMap(new Func1<LoginResp, Observable<LoginResp.DataBean>>() {
            @Override
            public Observable<LoginResp.DataBean> call(LoginResp resp) {
                return Observable.just(resp.getData());
            }
        }).subscribe(new Action1<LoginResp.DataBean>() {
            @Override
            public void call(LoginResp.DataBean dataBean) {

            }
        });
    }
}
